<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Models\User;
use Dcat\Admin\Admin;

class DataApiController
{
    public function getUserId()
    {
        $res = User::when(request('query'), function ($query, $value) {
            $query->where('telephone', 'like', "%{$value}%");
        })->get();
        if (blank($res)) {
            return Admin::json([]);
        }
        return Admin::json($res->toArray());
    }

    // 显示完整子树
    public function showSubtree($id)
    {
        $user = User::query()->findOrFail($id);

        $tree = $user->getDescendantsAndSelf()->toTree();

        return view('subtree', [
            'current' => $user,
            'tree' => $tree,
            'depth' => 111, // 初始展开层级
        ]);
    }

    // 动态加载子节点
    public function getChildren($id)
    {
        $children = Department::where('parent_id', $id)
            ->withCount('users')
            ->orderBy('_lft')
            ->get();

        return view('admin.partials.children', [
            'nodes' => $children,
            'level' => request('level', 0) + 1,
        ]);
    }
}
